package cn.xawl.gulimall.product.service.impl;

import cn.xawl.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.xawl.gulimall.product.entity.BrandEntity;
import cn.xawl.gulimall.product.entity.CategoryEntity;
import cn.xawl.gulimall.product.service.BrandService;
import cn.xawl.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.Query;

import cn.xawl.gulimall.product.dao.CategoryBrandRelationDao;
import cn.xawl.gulimall.product.entity.CategoryBrandRelationEntity;
import cn.xawl.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据品牌id查询该品牌关联的分类信息
     *
     * @param brandId 品牌id
     */
    @Override
    public List<CategoryBrandRelationEntity>  getCategoryListById(Long brandId) {

        return this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    /**
     * 保存详细关联信息，包括分类名字和品牌名字
     * @param categoryBrandRelation 参数信息
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //获取参数信息
        Long brandId = categoryBrandRelation.getBrandId();
        Long catalogId = categoryBrandRelation.getCatalogId();

        //分别根据id查询具体的信息(分类和品牌名字)
        CategoryEntity categoryEntity = categoryService.getById(catalogId);
        BrandEntity brandEntity = brandService.getById(brandId);

        //设置关联类中的名字信息
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatalogName(categoryEntity.getName());

        //保存关联信息
        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandEntity> getBrandListById(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = this.getBaseMapper().selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catalog_id", catId));

        return relationEntities.stream().map(relationEntity -> {
            Long brandId = relationEntity.getBrandId();
            return brandService.getById(brandId);
        }).collect(Collectors.toList());
    }
//    private Map<String, Object> dataMap(Object id, Object name) {
//        HashMap<String, Object> dataMap = new HashMap<>();
//        dataMap.put("catalogId", id);
//        dataMap.put("catalogName", name);
//        return dataMap;
//    }

}